package com.kehutong.department.controller;


import com.kehutong.department.entity.ThirdConfigure;
import org.coraframework.inject.Inject;
import org.coraframework.json.JSONObject;
import org.coraframework.mvc.MvcAction;
import org.coraframework.mvc.http.Bean;
import org.coraframework.mvc.http.ReqMapping;
import org.coraframework.mvc.http.result.Result;
import org.coraframework.orm.jdbc.JdbcSession;
import org.coraframework.orm.jdbc.Page;
import org.coraframework.orm.jdbc.execute.entity.FindPage;
import org.coraframework.util.Objects;

/**
 * 第三方外部连接分类配置
 * @author liulei
 */
@MvcAction
@ReqMapping("/admin/third/Connection/Classification")
public class ThirdConfigureController {
    @Inject
    private JdbcSession jdbcSession;
    @Bean(newInstance=true, copy=true)
    @ReqMapping("/save")
    public Object save(ThirdConfigure thirdConfigure) {

        // 增加校验不允许有相同的分类名称
        ThirdConfigure item = jdbcSession.findOne(ThirdConfigure.class)
                .eq("classificationName", thirdConfigure.getClassificationName())
                .eq("deleted", false)
                .exe();
        if(Objects.nonNull(item)){
            return Result.error(0, "已有当前分类名称，请修改后重试");
        }
        //插入分类
        jdbcSession.insert(thirdConfigure);
        return Result.success();
    }

    @Bean(copy=true)
    @ReqMapping("/update")
    public Object update(ThirdConfigure thirdConfigure) throws Exception {
        ThirdConfigure item = jdbcSession.findOne(ThirdConfigure.class)
                .eq("id", thirdConfigure.getId())
                .eq("deleted", false)
                .exe();
        if(item ==null){
            return Result.error(0, "数据不存在");
        }
        jdbcSession.updateById(thirdConfigure);
        return Result.success();
    }

    @ReqMapping("/delete")
    public Object delete(ThirdConfigure thirdConfigure) {
        thirdConfigure.setDeleted(true);
        jdbcSession.updateById(thirdConfigure);
        return Result.success();
    }
    @ReqMapping("/list")
    public Object list(JSONObject jsonObject) {
        FindPage<ThirdConfigure> find = jdbcSession.findPage(ThirdConfigure.class)
                .eq(jsonObject, "classificationName", "classificationName")
                .eq("deleted", false)
                .order("createTime", "DESC")
                .page(jsonObject);
        Page<ThirdConfigure> page=find.exe();
        return page;
    }

}
